<?php

	namespace Phptricks\system\library\facebook;

	/**
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 * Description of facebook
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\loaders\loader as loaders;
	use Phptricks\system\library\session\session as session;

	class facebook{

		private static $_instance = null;
		public $_app_id;
		public $_app_secret;
		public $_app_access_token;
		public $_RedirectLoginHelper;
		private $_loaders = null;
		private $_FacebookSession = null;
		private $_FacebookRedirectLoginHelper = null;
		private $_FacebookRequest = null;
		private $_App_Graph;
		private $_UserGraph;

		public function __construct($config){
			$this->_loaders = loaders::getinstance();
			session::getinstance();
			if (version_compare(PHP_VERSION, '5.4.0', '<')) {
				throw new Exception('The Facebook SDK v4 requires PHP version 5.4 or higher.');
			}
			$this->_loaders->_autoload(\SYSTEMPATH . 'library/facebook/', 'autoload');
			$this->_initialize($config);
		}

		public static function getinstance($config){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\facebook\facebook($config);
			}
			return self::$_instance;
		}

		public function _initialize($config){
			try {
				if (!isset($config)) {
					throw new Exc("You must enter a config data in param \$config");
				}
				if (!is_array($config)) {
					throw new Exc("this <strong>\$config</strong> is must be array not " . gettype($config));
				}
				$this->_app_id = $config['app_id'];
				$this->_app_secret = $config['app_secret'];
				$this->_app_access_token = $config['app_access_token'];
				$this->_RedirectLoginHelper = $config['RedirectLoginHelper'];
				\Facebook\FacebookSession::setDefaultApplication($this->_app_id, $this->_app_secret);
				$this->FacebookRedirectLoginHelper();
				$this->getSessionFromRedirect();
			} catch (\Facebook\FacebookRequestException $FBexc) {
				throw new Exc($FBexc->getMessage());
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		public function FacebookRedirectLoginHelper(){
			try {
				$this->_FacebookRedirectLoginHelper = new \Facebook\FacebookRedirectLoginHelper($this->_RedirectLoginHelper);
			} catch (\Facebook\FacebookRequestException $FBexc) {
				throw new Exc($FBexc->getMessage());
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		public function getSessionFromRedirect(){
			try {
				$this->_FacebookSession = $this->_FacebookRedirectLoginHelper->getSessionFromRedirect();
			} catch (\Facebook\FacebookRequestException $FBexc) {
				throw new Exc($FBexc->getMessage());
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		public static function login(){
			session::delete('fb_token');
		}

		public function is_login(){
			try {
				if (session::is_set('fb_token') === true) {
					$fb_token = session::get('fb_token');
					if (!empty($fb_token)) {
						$this->_FacebookSession = new \Facebook\FacebookSession($fb_token);
						$this->_app_access_token = $this->_FacebookSession->getToken();
						\Facebook\FacebookSession::newAppSession($this->_app_id, $this->_app_secret);
					} else {
						session::delete(array('fb_token', 'FBRLH_state'));
						return false;
					}
				} else {
					session::delete(array('fb_token', 'FBRLH_state'));
					return false;
				}
				if ($this->_FacebookSession != null) {
					if ($this->_FacebookSession->validate() != true) {
						session::delete(array('fb_token', 'FBRLH_state'));
						return false;
					}
					session::set('fb_token', $this->_app_access_token);
					$this->request();
					return true;
				} else {
					return false;
				}
			} catch (\Facebook\FacebookRequestException $FBexc) {
				throw new Exc($FBexc->getMessage());
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		public function request($type = 'GET', $node = 'me'){
			try {
				if (empty($type)) {
					$type = 'GET';
				}
				if (empty($node)) {
					$node = '/me';
				} else {
					$node = "/" . $node;
				}
				$this->_FacebookRequest = new \Facebook\FacebookRequest($this->_FacebookSession, $type, $node);
				$response = $this->_FacebookRequest->execute();
				$this->_App_Graph = $response->getGraphObject();
				$this->_UserGraph = $response->getGraphObject(\Facebook\GraphUser::className());
			} catch (\Facebook\FacebookRequestException $FBexc) {
				throw new Exc($FBexc->getMessage());
			} catch (\Exception $exc) {
				echo $exc->getMessage();
			}
		}

		public function getLoginUrl(){
			return $this->_FacebookRedirectLoginHelper->getLoginUrl();
		}

		public function getLogoutUrl(){
			return $this->_FacebookRedirectLoginHelper->getLogoutUrl($this->_FacebookSession, $this->_RedirectLoginHelper);
		}

		public function get_GraphUser(){
			pr($this->_UserGraph->getProperty('email'));
		}

		public function get_App_Graph(){
			$this->_App_Graph = new \Facebook\GraphObject();
		}

		public function __call($name, $arguments){
			pr($name);
			pr($arguments, 1);
		}

	}
	